package cn.ekuma.epos.order;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.SentenceList;
import com.openbravo.data.user.I_ListProvider;
import com.openbravo.data.user.ListProviderCreator;
import cn.ekuma.epos.datalogic.I_DataLogicCustomers;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.I_DataLogicReceipts;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.base.AppView;

import cn.ekuma.epos.datalogic.I_DataLogicSales;
import cn.ekuma.epos.order.model.OrderInfoTableModel;
import cn.ekuma.util.SwingUtil;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.bean.erp.viewbean.FindOrdersInfo;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Window;
import javax.swing.JFrame;
import javax.swing.ListSelectionModel;
import org.jdesktop.swingx.JXTable;

public class JSharedLinkOrdersFinder extends javax.swing.JDialog {

    private I_ListProvider lpr;
    private FindOrdersInfo selectedOrder;

    private JOrderFilter orderFilter;
    private boolean hide;
    JXTable jListOrders;

    /** Creates new form JCustomerFinder */
    private JSharedLinkOrdersFinder(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
    }

    /** Creates new form JCustomerFinder */
    private JSharedLinkOrdersFinder(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
    }
    
    public static JSharedLinkOrdersFinder getLinkOrderFinder(Component parent,AppView app,I_DataLogicReceipts dlReceipts,I_DataLogicERP dlSales,I_DataLogicCustomers dlCustomers) {
        Window window = getWindow(parent);
        
        JSharedLinkOrdersFinder myMsg;
        if (window instanceof Frame) { 
            myMsg = new JSharedLinkOrdersFinder((Frame) window, true);
        } else {
            myMsg = new JSharedLinkOrdersFinder((Dialog) window, true);
        }
        myMsg.init(app, dlReceipts, dlSales, dlCustomers);
        myMsg.applyComponentOrientation(parent.getComponentOrientation());
        return myMsg;
    }
    
    public FindOrdersInfo getSelectedOrder() {
        return selectedOrder;
    }

    private void init(AppView app,I_DataLogicReceipts dlReceipts,I_DataLogicERP dlSales,I_DataLogicCustomers dlCustomers) {
        hide = false;
        initComponents();
        orderFilter = new JOrderFilter(app, m_jKeys,dlSales,dlCustomers);
        jPanel9.add(orderFilter, BorderLayout.CENTER);

        //jtxtTicketID.activate();
        lpr = new ListProviderCreator(dlReceipts.getSharedLinkOrderListQBF(), orderFilter);

        jListOrders=new JXTable();
        jListOrders.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jListOrders.setModel(new OrderInfoTableModel(null));
        jListOrders.setAutoCreateRowSorter(true);
        jListOrders.setColumnControlVisible(true);
        jListOrders.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                if (evt.getClickCount() == 2) {
                    int selectedIndex=jListOrders.getSelectedRow();
                    if (selectedIndex != -1) {
                       selectedOrder = (FindOrdersInfo) ((OrderInfoTableModel)jListOrders.getModel()).getRow(jListOrders.convertRowIndexToModel(selectedIndex));
                     dispose();
                    }
                }
            }
        });
        jScrollPane1.setViewportView(jListOrders);
        getRootPane().setDefaultButton(jcmdOK);
        selectedOrder = null;
    }
    
    public void executeSearch() {
        try {
            jListOrders.setModel(new OrderInfoTableModel(lpr.loadData()));
            SwingUtil.PreferredJXTableColumn(jListOrders);
            if (jListOrders.getModel().getRowCount() > 0) {
                jListOrders.getSelectionModel().setSelectionInterval(0,0);
            }
        } catch (BasicException ex) {
            new MessageInf(ex).show(this);
        }        
    }
    
    

    private static Window getWindow(Component parent) {
        if (parent == null) {
            return new JFrame();
        } else if (parent instanceof Frame || parent instanceof Dialog) {
            return (Window) parent;
        } else {
            return getWindow(parent.getParent());
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jHide = new javax.swing.JButton();
        jPanel7 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel8 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jcmdOK = new javax.swing.JButton();
        jcmdCancel = new javax.swing.JButton();
        jPanel9 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        m_jKeys = new com.openbravo.editor.JEditorKeys();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(AppLocal.getIntString("form.tickettitle")); // NOI18N
        setMinimumSize(new java.awt.Dimension(500, 450));

        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel5.setLayout(new java.awt.BorderLayout());

        jButton1.setText(AppLocal.getIntString("button.clean")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel6.add(jButton1);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/launch.png"))); // NOI18N
        jButton3.setText(AppLocal.getIntString("button.executefilter")); // NOI18N
        jButton3.setFocusPainted(false);
        jButton3.setFocusable(false);
        jButton3.setRequestFocusEnabled(false);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel6.add(jButton3);

        jHide.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/1uparrow.png"))); // NOI18N
        jHide.setFocusable(false);
        jHide.setPreferredSize(new java.awt.Dimension(40, 25));
        jHide.setRequestFocusEnabled(false);
        jHide.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jHideActionPerformed(evt);
            }
        });
        jPanel6.add(jHide);

        jPanel5.add(jPanel6, java.awt.BorderLayout.SOUTH);

        jPanel7.setLayout(new java.awt.BorderLayout());
        jPanel5.add(jPanel7, java.awt.BorderLayout.CENTER);

        jPanel3.add(jPanel5, java.awt.BorderLayout.PAGE_START);

        jPanel4.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        jPanel4.setLayout(new java.awt.BorderLayout());
        jPanel4.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel3.add(jPanel4, java.awt.BorderLayout.CENTER);

        jPanel8.setLayout(new java.awt.BorderLayout());

        jcmdOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_ok.png"))); // NOI18N
        jcmdOK.setText(AppLocal.getIntString("Button.OK")); // NOI18N
        jcmdOK.setEnabled(false);
        jcmdOK.setFocusPainted(false);
        jcmdOK.setFocusable(false);
        jcmdOK.setMargin(new java.awt.Insets(8, 16, 8, 16));
        jcmdOK.setRequestFocusEnabled(false);
        jcmdOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcmdOKActionPerformed(evt);
            }
        });
        jPanel1.add(jcmdOK);

        jcmdCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_cancel.png"))); // NOI18N
        jcmdCancel.setText(AppLocal.getIntString("Button.Cancel")); // NOI18N
        jcmdCancel.setFocusPainted(false);
        jcmdCancel.setFocusable(false);
        jcmdCancel.setMargin(new java.awt.Insets(8, 16, 8, 16));
        jcmdCancel.setRequestFocusEnabled(false);
        jcmdCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcmdCancelActionPerformed(evt);
            }
        });
        jPanel1.add(jcmdCancel);

        jPanel8.add(jPanel1, java.awt.BorderLayout.LINE_END);

        jPanel3.add(jPanel8, java.awt.BorderLayout.SOUTH);

        getContentPane().add(jPanel3, java.awt.BorderLayout.CENTER);

        jPanel9.setLayout(new java.awt.BorderLayout());

        jPanel2.setPreferredSize(new java.awt.Dimension(0, 0));
        jPanel2.setLayout(new java.awt.BorderLayout());
        jPanel2.add(m_jKeys, java.awt.BorderLayout.NORTH);

        jPanel9.add(jPanel2, java.awt.BorderLayout.LINE_END);

        getContentPane().add(jPanel9, java.awt.BorderLayout.PAGE_START);

        getAccessibleContext().setAccessibleName("Orders list");

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-892)/2, (screenSize.height-684)/2, 892, 684);
    }// </editor-fold>//GEN-END:initComponents
    private void jcmdOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcmdOKActionPerformed
        int selectedIndex=jListOrders.getSelectedRow();
         if (selectedIndex != -1) {
              selectedOrder = (FindOrdersInfo) ((OrderInfoTableModel)jListOrders.getModel()).getRow(jListOrders.convertRowIndexToModel(selectedIndex));
         }
        dispose();
    }//GEN-LAST:event_jcmdOKActionPerformed

    private void jcmdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcmdCancelActionPerformed
        dispose();
    }//GEN-LAST:event_jcmdCancelActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        executeSearch();
    }//GEN-LAST:event_jButton3ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        orderFilter.defaultValues();
}//GEN-LAST:event_jButton1ActionPerformed

private void jHideActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jHideActionPerformed
    if(hide){
        hide=false;
        jPanel9.setVisible(true);
        jHide.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/1downarrow.png")));
    }else{
        hide=true;
        jPanel9.setVisible(false);
        jHide.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/1uparrow.png")));
    }
}//GEN-LAST:event_jHideActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jHide;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jcmdCancel;
    private javax.swing.JButton jcmdOK;
    private com.openbravo.editor.JEditorKeys m_jKeys;
    // End of variables declaration//GEN-END:variables
}